package com.hzncc.scaffold.controller.Front.IndustryNews;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzncc.scaffold.core.ResultT;
import com.hzncc.scaffold.entity.Other.Industrynews;
import com.hzncc.scaffold.entity.Vo.AttachFileVo;
import com.hzncc.scaffold.entity.Vo.FrontIndustryNewsDetailVo;
import com.hzncc.scaffold.entity.Vo.IndustryNewsVo;
import com.hzncc.scaffold.mapper.IndustrynewsMapper;
import com.hzncc.scaffold.service.IndustrynewsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

/**
 * @Author: DrunkBear
 * @Date: 2022-06-15 11:39
 * @Description: null
 */
@RequestMapping("/industryNews")
@Api(tags = "前台接口-产教融合")
@RestController
public class IndustryNewsController {

    @Autowired
    IndustrynewsMapper industrynewsMapper;

    @Autowired
    IndustrynewsService industrynewsService;


    @GetMapping("/list")
    @ApiOperation("产教融合列表")
    public ResultT list(Integer pageNum, Integer pageSize) {

        if (pageNum != null && pageSize != null && pageNum > 0 && pageSize > 0) {
            PageHelper.startPage(pageNum, pageSize);
        }
        List<IndustryNewsVo> industryNewsVoList = industrynewsMapper.list();
        HashMap<String,Object> map = new HashMap<>();
        map.put("rows",industryNewsVoList);
        map.put("count",new PageInfo<>(industryNewsVoList).getTotal());
        return ResultT.success(map);
    }


    @GetMapping("/detail")
    @ApiOperation("产教融合详情")
    public ResultT detail(Integer newsId) {
        if (industrynewsService.getById(newsId) == null) {
            return ResultT.fail("数据错误");
        }
        Industrynews industrynews = industrynewsService.getById(newsId);
        FrontIndustryNewsDetailVo frontIndustryNewsDetailVo = BeanUtil.copyProperties(industrynews, FrontIndustryNewsDetailVo.class, "attachFile");

        JSONArray jsonArray = new JSONArray(industrynews.getAttachFile());
        List<AttachFileVo> attachFileVoList = JSONUtil.toList(jsonArray, AttachFileVo.class);

        frontIndustryNewsDetailVo.setAttachFileVos(attachFileVoList);

        return ResultT.success(frontIndustryNewsDetailVo);
    }
}
